package org.jash.report.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import kotlinx.coroutines.launch
import org.jash.common.log.logDebug
import org.jash.common.mvi.BaseActivity
import org.jash.common.mvi.BaseState
import org.jash.report.R
import org.jash.report.databinding.ActivityCommitBinding
import org.jash.report.entity.ReportEntity
import org.jash.report.model.CommitIntent
import org.jash.report.model.CommitState
import org.jash.report.model.CommitViewModel

@Route(path = "/report/commit")
class CommitActivity : BaseActivity<ActivityCommitBinding, CommitViewModel>() {
    @Autowired
    @JvmField
    var reportType:Int = 0;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        binding.report = when(reportType) {
            1 -> ReportEntity(0, "明日工作计划", "", "今日工作总结", "", "张先生", 0, reportType)
            2 -> ReportEntity(0, "下周工作计划", "", "本周中工作总结", "", "张先生", 0, reportType)
            3 -> ReportEntity(0, "下月工作计划", "", "本月工作总结", "", "张先生", 0, reportType)
            else -> ReportEntity(0, "", "", "", "", "张先生", 0, reportType)
        }
        binding.type = when(reportType) {
            1 -> "日报"
            2 -> "周报"
            3 -> "月报"
            else -> ""
        }
        binding.submit.setOnClickListener {
            binding.report?.let { report ->
                logDebug(report)
                if (report.reportNowValue.isBlank()) {
                    Toast.makeText(this, "请填写完整的总结", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                if (report.reportNextValue.isBlank()) {
                    Toast.makeText(this, "请填写完整的计划", Toast.LENGTH_SHORT).show()
                    return@setOnClickListener
                }
                lifecycleScope.launch {
                    viewModel.intent.send(CommitIntent.Send(report))
                }
            }

        }

    }
    fun loaded(res :CommitState.ReportResponse) {
        Toast.makeText(this, "提交${if(res.flag) "成功" else "失败"}", Toast.LENGTH_SHORT).show()
    }
    fun error(error: BaseState.Error) {
        logDebug(error.msg)
        Toast.makeText(this, error.msg, Toast.LENGTH_SHORT).show()
    }
}